<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="不合格单号" prop="nonconformingNo">
        <el-input v-model="searchForm.nonconformingNo" placeholder="请输入不合格单号" clearable />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="searchForm.productName" placeholder="请输入产品名称" clearable />
      </el-form-item>
      <el-form-item label="不合格类型" prop="nonconformingType">
        <el-select v-model="searchForm.nonconformingType" placeholder="请选择类型" clearable style="width: 180px">
          <el-option label="尺寸不合格" value="DIMENSION" />
          <el-option label="外观不合格" value="APPEARANCE" />
          <el-option label="功能不合格" value="FUNCTION" />
          <el-option label="性能不合格" value="PERFORMANCE" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="待处理" value="PENDING" />
          <el-option label="处理中" value="PROCESSING" />
          <el-option label="已处理" value="COMPLETED" />
          <el-option label="已关闭" value="CLOSED" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增不合格品</el-button>
      <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="nonconformingNo" label="不合格单号" />
      <el-table-column prop="productCode" label="产品编码" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="batchNo" label="批次号" />
      <el-table-column prop="nonconformingType" label="不合格类型">
        <template #default="scope">
          <el-tag :type="getTypeTagType(scope.row.nonconformingType)">{{ getTypeText(scope.row.nonconformingType) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="nonconformingQuantity" label="不合格数量" align="right">
        <template #default="scope">
          <span class="nonconforming-quantity">{{ scope.row.nonconformingQuantity }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="severity" label="严重程度">
        <template #default="scope">
          <el-tag :type="getSeverityType(scope.row.severity)">{{ getSeverityText(scope.row.severity) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="disposalMethod" label="处置方式">
        <template #default="scope">
          <el-tag>{{ getDisposalText(scope.row.disposalMethod) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="处理状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="reporter" label="报告人" />
      <el-table-column prop="reportTime" label="报告时间" />
      <el-table-column prop="handler" label="处理人" />
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看详情</el-button>
          <el-button type="warning" link @click="handleProcess(scope.row)" v-if="scope.row.status !== 'COMPLETED'">
            处理
          </el-button>
          <el-button type="success" link @click="handleClose(scope.row)" v-if="scope.row.status === 'COMPLETED'">
            关闭
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="nonconformingProduct">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { CirclePlus, Download } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  nonconformingNo: "",
  productName: "",
  nonconformingType: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取不合格类型标签类型
const getTypeTagType = (type: string) => {
  const typeMap: Record<string, string> = {
    DIMENSION: "warning",
    APPEARANCE: "info",
    FUNCTION: "danger",
    PERFORMANCE: "primary"
  };
  return typeMap[type] || "info";
};

// 获取不合格类型文本
const getTypeText = (type: string) => {
  const typeMap: Record<string, string> = {
    DIMENSION: "尺寸不合格",
    APPEARANCE: "外观不合格",
    FUNCTION: "功能不合格",
    PERFORMANCE: "性能不合格"
  };
  return typeMap[type] || type;
};

// 获取严重程度类型
const getSeverityType = (severity: string) => {
  const severityMap: Record<string, string> = {
    LOW: "success",
    MEDIUM: "warning",
    HIGH: "danger",
    CRITICAL: "danger"
  };
  return severityMap[severity] || "info";
};

// 获取严重程度文本
const getSeverityText = (severity: string) => {
  const severityMap: Record<string, string> = {
    LOW: "轻微",
    MEDIUM: "一般",
    HIGH: "严重",
    CRITICAL: "致命"
  };
  return severityMap[severity] || severity;
};

// 获取处置方式文本
const getDisposalText = (method: string) => {
  const methodMap: Record<string, string> = {
    REWORK: "返工",
    REPAIR: "返修",
    SCRAP: "报废",
    CONCESSION: "让步接收",
    DOWNGRADE: "降级使用"
  };
  return methodMap[method] || method;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    PENDING: "warning",
    PROCESSING: "primary",
    COMPLETED: "success",
    CLOSED: "info"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    PENDING: "待处理",
    PROCESSING: "处理中",
    COMPLETED: "已处理",
    CLOSED: "已关闭"
  };
  return statusMap[status] || status;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = [
      {
        id: 1,
        nonconformingNo: "NC202401150001",
        productCode: "FP001",
        productName: "智能手机X1",
        batchNo: "B20240115001",
        nonconformingType: "APPEARANCE",
        nonconformingQuantity: 5,
        severity: "MEDIUM",
        disposalMethod: "REWORK",
        status: "PROCESSING",
        reporter: "张三",
        reportTime: "2024-01-15 10:30:00",
        handler: "李四"
      },
      {
        id: 2,
        nonconformingNo: "NC202401150002",
        productCode: "FP002",
        productName: "工业机器人A1",
        batchNo: "B20240115002",
        nonconformingType: "FUNCTION",
        nonconformingQuantity: 2,
        severity: "HIGH",
        disposalMethod: "REPAIR",
        status: "COMPLETED",
        reporter: "王五",
        reportTime: "2024-01-15 14:15:00",
        handler: "赵六"
      },
      {
        id: 3,
        nonconformingNo: "NC202401150003",
        productCode: "FP003",
        productName: "精密仪器C1",
        batchNo: "B20240115003",
        nonconformingType: "DIMENSION",
        nonconformingQuantity: 1,
        severity: "CRITICAL",
        disposalMethod: "SCRAP",
        status: "PENDING",
        reporter: "孙七",
        reportTime: "2024-01-15 16:45:00",
        handler: ""
      }
    ];
    
    tableData.value = mockData;
    pagination.total = mockData.length;
  } catch (error) {
    console.error("获取不合格品记录失败:", error);
    ElMessage.error("获取不合格品记录失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 新增不合格品
const handleAdd = () => {
  ElMessage.info("新增不合格品记录功能开发中");
};

// 导出
const handleExport = () => {
  ElMessage.info("导出功能开发中");
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info(`查看不合格单 ${row.nonconformingNo} 详情`);
};

// 处理不合格品
const handleProcess = (row: any) => {
  ElMessage.info(`处理不合格单 ${row.nonconformingNo}`);
};

// 关闭不合格品
const handleClose = (row: any) => {
  ElMessage.info(`关闭不合格单 ${row.nonconformingNo}`);
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.nonconforming-quantity {
  color: #f56c6c;
  font-weight: bold;
}
</style>